package com.ztom.top100;

/**
 * 寻找重复数
 * <p>
 * https://leetcode-cn.com/problems/find-the-duplicate-number/
 *
 * @author ZhangTao
 */
public class Code74FindDuplicate {

    public int findDuplicate(int[] nums) {
        // 像链表一样处理: 快慢指针找环入口
        int slow = 0;
        int fast = 0;
        do {
            slow = nums[slow];
            fast = nums[nums[fast]];
        } while (slow != fast);
        slow = 0;
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
        }
        return slow;
    }
}
